<template>
  <div>
    <a-modal :visible="visible" :destroyOnClose="true" title="库存调拨" @ok="submit" @cancel="close" :maskClosable="false" width="70vw">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="条码">
                <a-input v-model="queryParam.sku" placeholder="请输入条码" allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="数量">
                <a-input-number v-model="queryParam.qty" placeholder="请输入数量" :min="0" style="width:150px;" allowClear />
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'left', overflow: 'hidden' }">
                <a-button type="primary" @click="add">添加</a-button>
                <!-- <a-button style="margin-left: 8px" type="primary" @click="handleImportXls">导入</a-button> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table
        :columns="columns"
        ref="table"
        size="middle"
        :rowKey="record => JSON.stringify(record)"
        :dataSource="dataSource"
        :pagination="false"
        :rowClassName="record => (record.qty <= record.transInNum && record.qty <= record.stock ? null : 'red')"
      >
        <span slot="action" slot-scope="text, record">
          <a-input-number v-model="record.qty" style="width:100px;margin-right:8px" :min="0" />
          <a-icon type="exclamation-circle" v-if="warning(record)" theme="twoTone" two-tone-color="#eb2f96" style="font-size:16px;" />
        </span>
      </a-table>
    </a-modal>
    <j-import-modal ref="importModal" :uploadParams="getUploadParams()" @ok="importOk" :templateUrl="templateUrl()" :serviceId="6"></j-import-modal>
  </div>
</template>
<script>
import JImportModal from '@/components/qiniuUpload/JImportModal'
import globalData from '@/assets/js/common'
import { queryUploadSuccess } from '@/components/qiniuUpload/common'
import { getTransGoodsInfo, allotsBox2Store, getBoxShopStock } from '@/api/box/setting'
let columns = [
  {
    title: '商品名',
    dataIndex: 'name'
  },
  {
    title: '条码',
    dataIndex: 'sku'
  },
  {
    title: '颜色',
    dataIndex: 'colorNo',
    customRender: (text, record) => `${text}/${record.colorName}`
  },
  {
    title: '尺码',
    dataIndex: 'size'
  },
  {
    title: '当前调入数',
    dataIndex: 'transInNum'
  },
  {
    title: '当前库存',
    dataIndex: 'stock'
  },
  {
    title: '调出数',
    width: 180,
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: { JImportModal },
  data() {
    return {
      columns,
      visible: false,
      timer: null,
      queryParam: {},
      storeId: null,
      dataSource: []
    }
  },
  computed: {
    warning() {
      return record => {
        if (record.qty <= record.stock && record.qty <= record.transInNum) {
          return false
        } else {
          return true
        }
      }
    }
  },
  methods: {
    show(id) {
      this.storeId = id
      this.queryParam = {}
      this.dataSource = []
      this.visible = true
    },
    close() {
      this.visible = false
    },
    add() {
      if (!this.queryParam.sku) {
        this.$message.warning('请输入条码')
        return
      }
      if (!this.queryParam.qty) {
        this.$message.warning('请输入数量')
        return
      }
      let hasSku = this.dataSource.filter(e => e.sku == this.queryParam.sku)
      if (hasSku.length > 0) {
        this.$message.warning('已存在' + this.queryParam.sku)
        return
      }
      let params = {
        requestData: {
          sku: this.queryParam.sku,
          storeId: this.storeId
        }
      }
      getTransGoodsInfo(params).then(res => {
        if (res.code == 0) {
          let data = { ...res.data, qty: this.queryParam.qty }
          this.dataSource.push(data)
          this.fetchBoxShopStock([res.data.sku])
          this.queryParam = {}
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    fetchBoxShopStock(list) {
      let params = {
        requestData: list
      }
      getBoxShopStock(params).then(res => {
        if (res.code == 0) {
          console.log(this.dataSource)
          for (let item of this.dataSource) {
            let result = res.data.filter(e => e.skuNo == item.sku)
            if (result.length > 0) {
              item.stock = result[0].qty
            }
          }
          this.$forceUpdate()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 导入
    handleImportXls() {
      this.$refs.importModal.show()
    },
    getUploadParams() {
      return { ...globalData.uploadParams }
    },
    templateUrl() {
      return 'https://img.bzhz.jnbygroup.com/cusLabelTemp.xlsx'
    },
    importOk(result, fileList) {
      console.log(fileList)
      let that = this
      queryUploadSuccess(this.timer, result, 60000, function(x, data) {
        console.log(x, data)
        if (x == 'done' || x == 'warning') {
          that.$refs.importModal.close()
        }
        if (x == 'done') {
          console.log(fileList, data)
          if (!data.url) {
            // that.fileName = fileList[0].split('-')[1]
            // that.key = result
            this.$message.success('导入成功！')
          }
        }
      })
    },
    submit() {
      let result = this.dataSource.filter(item => !(item.qty <= item.stock && item.qty <= item.transInNum))
      if (result.length > 0) {
        this.$message.warning('有商品调出数大于当前调入数和当前库存!')
        return
      }
      let allEmpty = this.dataSource.filter(e => e.qty == 0)
      if (allEmpty.length === this.dataSource.length) {
        this.$message.warning('禁止所有商品调出数为零!')
        return
      }
      let list = this.dataSource.map(e => {
        return {
          qty: e.qty,
          sku: e.sku,
          storeId: e.storeId
        }
      })

      let params = {
        requestData: {
          productList: list,
          storeId: this.storeId
        }
      }
      allotsBox2Store(params).then(res => {
        if (res.code == 0) {
          this.$message.warning('调拨成功')
        } else {
          this.$message.warning(res.msg)
          let arr = this.dataSource.map(e => e.sku)
          this.fetchBoxShopStock(arr)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/.red {
  color: red;
}
</style>

